package com.ztc.ZtcCar.dao;

import com.aqgj.util.Pager;
import com.ztc.ZtcCar.bean.ZtcCar;
import com.ztc.ZtcCar.dao.ZtcCarDao;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Repository;
import com.aqgj.util.BaseDaoImpl;


@Repository("ZtcCarDao")
public class ZtcCarDao extends BaseDaoImpl<ZtcCar> {

    //保存车信息
    public String save(ZtcCar ztcCar) {
        this.getHibernateTemplate().save(ztcCar);
        return ztcCar.getId();
    }

    //根据公司id、是否通过审核的状态获取旗下车信息.
    public Pager<ZtcCar> getZtcCarByIdAndReviewed(String transportId, boolean isReviewed,Integer pageSize,Integer pageNumber,String search,Integer type) {
        Pager<ZtcCar> pager = new Pager<>();
        pager.setPageSize(pageSize);
        pager.setCurrentPage(pageNumber);
        String sql = "";
        if (isReviewed) {
            sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and c.parent_id='" + transportId + "' and z.`status` <> 2 and z.status<>5 and z.type=1 ";
            if(type==0){
                sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and z.status <> 2 and z.status<>5 and z.type=1 ";
            }
            if (search != null && !"".equals(search)) {
                sql += "and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }

        } else {
            sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and c.parent_id='" + transportId + "' and z.`status`=2 and z.type=1 ";
            if(type==0){
                sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and z.status=2 and z.type=1 ";
            }
            if (search != null && !"".equals(search)) {
                sql += "and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }

        }
        pager.setRecordTotal(this.currentSession().createNativeQuery(sql, ZtcCar.class).getResultList().size());
        sql += " limit ?,?";
        pager.setContent(this.currentSession().createNativeQuery(sql, ZtcCar.class).setParameter(1, pager.getStart()).setParameter(2, pageSize).getResultList());
        return pager;
    }

    //根据公司id获取旗下车或车方车(运营公司获取旗下车已审核的车和本公司车)
    public Pager<ZtcCar> getCarListByTransportId(String transportId, Integer pageSize, Integer pageNumber, Integer transportType,String search) {
        Pager<ZtcCar> pager = new Pager<>();
        pager.setPageSize(pageSize);
        pager.setCurrentPage(pageNumber);
        String sql = "select z.* from ztc_car z where z.transport_id='" + transportId + "'";
        String countSql = "select count(*) from ztc_car z where z.transport_id='" + transportId + "'";
        //平台公司
        if(transportType==0){
            sql = "select z.* from ztc_car z where z.status<>2 and z.status<>5 and z.type=1 ";
            countSql = "select count(*) from ztc_car z where z.status<>2 and z.status<>5 and z.type=1 ";
            if (search != null && !"".equals(search)) {
                sql ="select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and z.status<>2 and z.status<>5 and z.type=1 and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
                countSql ="select count(*) from ztc_car z,ztc_transport c where z.transport_id=c.id and z.status<>2 and z.status<>5 and z.type=1 and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }
        //运营公司
        }else if (transportType == 3) {
            sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and (z.transport_id='" + transportId + "' or (c.parent_id='" + transportId + "' and z.`status`<>2 and z.status<>5)) and z.type=1 ";
            countSql += " or (z.transport_id in (select c.id from ztc_transport c where c.parent_id='" + transportId + "') and  z.`status`<>2 and z.status<>5) and z.type=1 ";
            if (search != null && !"".equals(search)) {
                sql += "and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }
        } else{//非运营（车方）
            if (search != null && !"".equals(search)) {
                sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and z.transport_id='" + transportId + "' and z.type=1 and (z.brand like '%" + search + "%' or z.car_number like '%" + search + "%' or z.property like '%" + search + "%' or z.color like '%" + search + "%' or z.type like '%" + search + "%' or z.seat_num like '%" + search + "%' or c.full_name like '%" + search + "%')";
            }
        }
        sql += " limit ?,?";
        pager.setContent(this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql, ZtcCar.class).setParameter(1, pager.getStart()).setParameter(2, pageSize).getResultList());
        pager.setRecordTotal(((BigInteger) this.currentSession().createNativeQuery(countSql).getResultList().get(0)).intValue());
        return pager;
    }

    public String update(ZtcCar ztcCar) {
        this.currentSession().merge(ztcCar);
        return ztcCar.getId();
    }

    public List<ZtcCar> getCarListByTransportId(String transportId,Integer transportType) {
        String sql = "select z.* from ztc_car z where z.transport_id='" + transportId + "' and z.`status`=1 and z.type=1 ";
        if (transportType == 3) {//运营公司
            sql = "select z.* from ztc_car z,ztc_transport c where z.transport_id=c.id and (z.transport_id='" + transportId + "' or c.parent_id='" + transportId + "')  and z.`status`=1 and z.type=1 ";
        }
        return this.getHibernateTemplate().getSessionFactory().getCurrentSession().createNativeQuery(sql, ZtcCar.class).getResultList();
    }
}

